package reflect;

import java.lang.reflect.Constructor;

public class ReflectDemo3 {
    public static void main(String[] args) throws Exception {
        Person p = new Person("张三");
        System.out.println(p);

        //反射机制获取对象
        //1、获取类对象
        Class cls=Class.forName("reflect.Person");
        //Constructor cons=cls.getConstructor();//获取无参构造器
        Constructor cons=cls.getConstructor(String.class);//获取有参构造器Persong（String）
        Object obj=cons.newInstance("李四");//new persong("李四")
        System.out.println(obj);

        Constructor cons2=cls.getConstructor(String.class,int.class);
        Object obj2=cons2.newInstance("王五",20);
        System.out.println(obj2);


    }
}
